package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


@Slf4j
@Service

public class DishServiceImpl implements DishService {

    @Autowired
    public DishMapper dishMapper;

    @Autowired
    public DishFlavorMapper dishFlaverMapper;

    @Autowired
    public SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNewDish(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.addNewDish(dish);
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();

        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(id);
        });

        if(!flavors.isEmpty()){
            dishFlaverMapper.insertFlavors(flavors);
        }

    }

    @Override
    public PageResult selectDishByPage(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        //2. 执行查询
        List<Dish> dishList = dishMapper.selectEmpByPage(dishPageQueryDTO);

        //3. 解析查询结果, 并封装
        Page<Dish> p = (Page<Dish>) dishList;
        return new PageResult((int) p.getTotal(), p.getResult());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOneDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.updateOneDish(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlaverMapper.deleteByDishId(dish.getId());

        if(!flavors.isEmpty()){
            dishFlaverMapper.insertFlavors(flavors);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DishDTO selectOneDish(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishDTO dishDTO = new DishDTO();

        BeanUtils.copyProperties(dish, dishDTO);

        List<DishFlavor> dishFlavorMapper = dishFlaverMapper.getDishFlavorById(id);
        dishDTO.setFlavors(dishFlavorMapper);

        return dishDTO;
    }


    @Override
    public void deleteDishById(List<Integer> ids) {
//        判断当前菜品能不能被删除，是否是起售中的菜品


//        判断当前的菜品能不能被删除，是否关联了套餐
        ids.forEach(id -> {
            Dish dish =  dishMapper.selectById(Long.valueOf(id));

            if(Objects.equals(dish.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(dish.getName() + MessageConstant.DISH_ON_SALE);
            }

        });

        List<Long> set_meals = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(set_meals != null && !set_meals.isEmpty()){
            throw new DeletionNotAllowedException( set_meals + MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        dishMapper.deleteDishByIds(ids);

        ids.forEach(id -> {
            dishFlaverMapper.deleteByDishId(Long.valueOf(id));
        });
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();


        dishMapper.updateOneDish(dish);

    }

    @Override
    public List<DishDTO> selectDishByCategoryId(Long categoryId) {

        return dishMapper.selectDishByCategoryId(categoryId);
    }


}
